package model;

import java.util.ArrayList;
import java.util.List;

import common.ItemFactory;
import controller.Controller;

/**
 * @author Colin
 * 
 * The Cargo class specifies the manual weapons that
 * the user has already purchased.
 *
 */
public class Cargo {
	
	private List<ManualWeapon> manualWeapons;
	private Asteroid asteroid;
	private SmallCanon smallCanon;
	private LargeCanon largeCanon;
	private SmallHomingMissile smallHomingMissile;
	private LargeHomingMissile largeHomingMissile;
	
	private boolean weaponSelected = false;
	private String selectedWeapon = "";
	
	public Cargo(){
		manualWeapons = new ArrayList<ManualWeapon>();
		weaponSelected = false;
		selectedWeapon = "";
		
		smallCanon = new SmallCanon();
		largeCanon = new LargeCanon();
		asteroid = new Asteroid();
		smallHomingMissile = new SmallHomingMissile();
		largeHomingMissile = new LargeHomingMissile();
		
		manualWeapons.add(smallCanon);
		manualWeapons.add(largeCanon);
		manualWeapons.add(asteroid);
		manualWeapons.add(smallHomingMissile);
		manualWeapons.add(largeHomingMissile);
	}
	
	public boolean selectWeapon( String weaponName ){
		ManualWeapon weapon = getManualWeapon(weaponName);
		System.out.println("ManualWeapon ammo size " + weapon.getAmmo().size());
		
		if (weapon.getAmmo().size() > 0){
			selectedWeapon = weaponName;
			weaponSelected = true;
		}
		else{
			deselectWeapon();
		}
		Controller.updateView();
		return weaponSelected;
	}
	
	public boolean isWeaponSelected(){
		return weaponSelected;
	}
	
	public String getSelectedWeapon(){
		return selectedWeapon;
	}
	
	public void deselectWeapon(){
		weaponSelected = false;
		selectedWeapon = "";
		Controller.updateView();
	}
	
	public List<ManualWeapon> getManualWeapons() {
		return manualWeapons;
	}

	public void setManualWeapons(List<ManualWeapon> manualWeapons) {
		this.manualWeapons = manualWeapons;
	}
	
	public void addAmmo( ManualWeapon weapon ){
		weapon.addAmmo();
		System.out.println(weapon.getName() + " has " + weapon.getAmmo().size() + " ammo remaining");
	}
	
	public Ammo removeAmmo( ManualWeapon weapon ){
		Ammo ammo = weapon.useAmmo();
		System.out.println(weapon.getName() + " has " + weapon.getAmmo().size() + " ammo remaining");
		if(weapon.getAmmo().size() <= 0){
			selectedWeapon = "";
			weaponSelected = false;
		}
		return ammo;
	}
	
	public ManualWeapon getManualWeapon( String manWeaponName ){
		ManualWeapon manWeapon = (ManualWeapon) ItemFactory.createItemFromImageName(manWeaponName);
		if(manWeapon instanceof Asteroid){
			return asteroid;
		}
		else if(manWeapon instanceof SmallCanon){

			return smallCanon;
		}
		else if(manWeapon instanceof LargeCanon){
			return largeCanon;
		}
		else if(manWeapon instanceof SmallHomingMissile){
			return smallHomingMissile;
		}
		else if(manWeapon instanceof LargeHomingMissile){
			return largeHomingMissile;
		}
		else 
			return null;
	}
	
}
